package com.jw.qme.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jw.qme.domain.dto.GroupDTO;
import com.jw.qme.domain.po.Group;
import com.jw.qme.domain.po.GroupMember;
import com.jw.qme.domain.vo.GroupVO;
import com.jw.qme.mapper.GroupMapper;
import com.jw.qme.service.GroupMemberService;
import com.jw.qme.service.GroupService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * ClassName: com.jw.qme.service.impl
 * Datetime: 2025/4/4 下午7:31
 * Description:
 *
 * @author 汪佳泉
 * @version 1.0
 * @since 1.0
 */
@Service
@RequiredArgsConstructor
public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group>
        implements GroupService {

    private final GroupMemberService groupMemberService;

    @Override
    @Transactional
    public Boolean createGroup(GroupDTO groupDto) {
        // 从上下文中获取 创建用户信息
        String username = (String) StpUtil.getLoginId();
        // 使用UUID作为群聊ID
        String groupId = UUID.randomUUID().toString();
        boolean saveGroup = save(Group.builder().groupId(groupId).groupName(groupDto.getGroupName()).description(groupDto.getDescription()).createdBy(username).build());
        // 通过上面的信息进行查询
        // 添加一条全成员记录
        boolean saveGroupMember = groupMemberService.save(new GroupMember(groupId, (String) StpUtil.getLoginId(), "admin"));
        // 添加其他群聊人员记录
        ArrayList<GroupMember> groupMembers = new ArrayList<>();
        for (String userName : groupDto.getUserNames()) {
            GroupMember member = new GroupMember(groupId,userName,"member");
            groupMembers.add(member);
        }
        boolean saveGroupMembers = groupMemberService.saveBatch(groupMembers);
        return saveGroup && saveGroupMember && saveGroupMembers;
    }

    @Override
    public GroupVO getGroupByNo(String groupId) {
        Group group = getOne(new LambdaQueryWrapper<Group>().eq(Group::getGroupId, groupId));
        GroupVO groupVO = new GroupVO();
        BeanUtils.copyProperties(group,groupVO);
        return groupVO;
    }

    @Transactional
    @Override
    public Boolean deleteGroup(String groupId) {
        // 先进行查询
        Group group = getOne(new LambdaQueryWrapper<Group>().eq(Group::getGroupId, groupId));
        if(group == null) return false;
        remove(new LambdaQueryWrapper<Group>().eq(Group::getGroupId, groupId));
        groupMemberService.remove(new LambdaQueryWrapper<GroupMember>().eq(GroupMember::getGroupId, group.getGroupId()));
        return true;
    }

    @Override
    public List<GroupVO> getAllGroups(String username) {
        return groupMemberService.getGroupsByUsername(username);
    }

    @Override
    public Boolean joinGroup(String groupId) {
        String username = (String) StpUtil.getLoginId();
        // 先查询本用户是否在这个群聊中 如果在这个群聊中就不做处理
        GroupMember one = groupMemberService.getOne(new LambdaQueryWrapper<GroupMember>().eq(GroupMember::getGroupId, groupId).eq(GroupMember::getUsername, username));
        if(one != null) return true;
        return groupMemberService.save(new GroupMember(groupId, username, "member"));
    }

}
